﻿using Snblog.Jwt;

namespace Snblog.Controllers;

/// <summary>
/// 博客设置
/// </summary>
[Route("api/[controller]")]
[ApiExplorerSettings(GroupName = "V1")] //版本控制
[ApiController]
//  [Authorize]
public class SnSetBlogController : ControllerBase
{
    private readonly ISnSetBlogService _service; //IOC依赖注入
    #region 构造函数
    /// <summary>
    /// 构造函数
    /// </summary>
    public SnSetBlogController(ISnSetBlogService service)
    {
        _service = service ?? throw new ArgumentNullException(nameof(service));
    }
    #endregion

    #region 查询总数 GetCountAsync
    /// <summary>
    /// 查询总数
    /// </summary>
    /// <param name="identity">所有:0 || 分类:1 || 用户2  </param>
    /// <param name="type">条件(identity为0则填0) </param>
    /// <param name="cache"></param>
    [HttpGet("GetSunAsync")]
    public async Task<IActionResult> GetCountAsync(int identity = 0, string type = "null", bool cache = false)
    {
        return Ok(await _service.GetCountAsync(identity, type, cache));
    }
    #endregion

    #region 分页查询GetFyAsync
    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="identity">所有:0 || 分类:1 || 用户:2</param>
    /// <param name="type">类别参数, identity 0 可不填</param>
    /// <param name="pageIndex">当前页码</param>
    /// <param name="pageSize">每页记录条数</param>
    /// <param name="isDesc">是否倒序[true/false]</param>
    /// <param name="cache">是否开启缓存</param>
    /// <param name="ordering">排序条件[id排序]</param>
    /// <returns></returns>
    [HttpGet("GetPagingAsync")]
    public async Task<IActionResult> GetFyAsync(
        int identity = 0,
        string type = "null",
        int pageIndex = 1,
        int pageSize = 10,
        string ordering = "id",
        bool isDesc = true,
        bool cache = false
    )
    {
        return Ok(await _service.GetFyAsync(identity, type, pageIndex, pageSize, ordering, isDesc, cache));
    }
    #endregion

    #region 主键查询
    /// <summary>
    /// 主键查询
    /// </summary>
    /// <param name="id">文章id</param>
    /// <param name="cache">是否开启缓存</param>
    /// <returns></returns>
    [HttpGet("GetByIdAsync")]
    public async Task<IActionResult> GetByIdAsync(int id, bool cache = false)
    {
        return Ok(await _service.GetByIdAsync(id, cache));
    }
    #endregion

    #region 添加数据
    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [Authorize(Policy = JPermissions.Create)]
    [HttpPost("AddAsync")]
    public async Task<IActionResult> AddAsync(SnSetblogDto entity)
    {
        return Ok(await _service.AddAsync(entity));
    }
    #endregion
    #region 更新数据
    /// <summary>
    /// 更新数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [Authorize(Policy = JPermissions.Edit)]
    [HttpPut("UpdateAsync")]
    public async Task<IActionResult> UpdateAsync(SnSetblogDto entity)
    {
        return Ok(await _service.UpdateAsync(entity));
    }
    #endregion
    #region 删除数据
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [Authorize(Policy = JPermissions.Delete)]
    [HttpDelete("DelAsync")]
    public async Task<IActionResult> DeleteAsync(int id)
    {
        return Ok(await _service.DeleteAsync(id));
    }
    #endregion
    #region 更新部分列[comment give read]
    /// <summary>snblog
    /// 更新部分列
    /// </summary>
    /// <param name="entity">对象</param>
    /// <param name="type">更新字段 type</param>
    /// <returns></returns>
    [HttpPut("UpdatePortionAsync")]
    public async Task<IActionResult> UpdatePortionAsync(SnSetblogDto entity, string type)
    {
        return Ok(await _service.UpdatePortionAsync(entity, type));
    }
    #endregion
}
